package org.gmoting.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.gmoting.exif.Messages;

public class Util {

	public static final String EXT_JPG_MAY = ".JPG";
	public static final String EXT_JPG_MIN = ".jpg";

	/**
	 * Leer un fichero a un array de bytes.
	 * 
	 * @param file
	 *            Ruta del fichero
	 * @return El fichero en un array de bytes.
	 * @throws IOException
	 */
	public static byte[] getBytesFromFile(File file) throws IOException {
		InputStream is = new FileInputStream(file);
		long length = file.length();
		if (length > Integer.MAX_VALUE) {
			throw new RuntimeException(Messages.getString("error.fileTooLong"));
		}
		byte[] bytes = new byte[(int) length];
		int offset = 0;
		int numRead = 0;
		while (offset < bytes.length
				&& (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
			offset += numRead;
		}
		if (offset < bytes.length) {
			throw new IOException(Messages
					.getString("error.fileNotReadCompletely")
					+ file.getName());
		}
		is.close();
		return bytes;
	}
}
